package br.pucrio.inf.tamdera.prolog;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.util.EList;

import br.pucrio.inf.tamdera.ConceptDeclaration;
import br.pucrio.inf.tamdera.Constants;
import br.pucrio.inf.tamdera.DependencyRuleDeclaration;
import br.pucrio.inf.tamdera.Paragraph;
import br.pucrio.inf.tamdera.RulesSpecification;
import br.pucrio.inf.tamdera.prolog.utils.PrologIntegrationConstants;

public class PrologAntiErosionDesignRuleExtractorVisitor {

	public PrologAntiErosionDesignRuleExtractorVisitor() 
	{
		designRuleLogicalFacts = new ArrayList<String>();
		
	}
	protected List<String> designRuleLogicalFacts;

	protected void _extractDesignRules(final RulesSpecification spec) 
	{
		EList<Paragraph> paragraphs = spec.getParagraphs();
		for (Paragraph para : paragraphs )
		{
			extractDesignRules( para );
		}
	}

	protected void _extractDesignRules(final DependencyRuleDeclaration dependDecl) 
	{
		ConceptDeclaration source =  dependDecl.getSource();
		ConceptDeclaration target =  dependDecl.getTarget();
		
		String sourceName = source.getName();
		String targetName = target.getName();
		
		//cannot, only-can, 
		String type = dependDecl.getType();
		if (type.equals( Constants.DEPENDENCY_TYPE_CAN))
		{
			type = Constants.DEPENDENCY_TYPE_ONLY_CAN;
		}
		
		//relation
		String relation = dependDecl.getRelation();
		
		String template = getTemplateOfQuery( relation, type );
		
		String logicalFact = String.format ( template, type, relation, 
				sourceName, targetName );
		
		designRuleLogicalFacts.add( logicalFact );
	}
	
	/**
	 * Some relations type needs more parameters than 
	 * @param relationType
	 * @param type 
	 * @return the template of a query considering the type of relation 
	 */
	public String getTemplateOfQuery ( String relationType, String dependencyType )
	{
		String templateQuery =  PrologIntegrationConstants.ANTI_EROSION_DESIGN_RULE_FACT;
		return templateQuery;
	}

	public void extractDesignRules(final Object object) 
	{
		if (object instanceof DependencyRuleDeclaration)
		{
			_extractDesignRules((DependencyRuleDeclaration)object);
		} 
		else if (object instanceof RulesSpecification) 
		{
			_extractDesignRules((RulesSpecification)object);
		} 
		else 
		{
		}
	}

	public List<String> getDesignRuleLogicalFacts() {
		return designRuleLogicalFacts;
	}

	public void setDesignRuleLogicalFacts(List<String> designRuleLogicalFacts) {
		this.designRuleLogicalFacts = designRuleLogicalFacts;
	}
	
}
